package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.AdvertisingPlace;
import com.example.common.util.R;
import com.example.buildtable.pojo.Advertisement;
import com.example.buildtable.service.AdvertisementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "广告")
@RequestMapping(value = "/advertisement/")
public class AdvertisementController {
    @Autowired
    private AdvertisementService advertisementService;
    
    @Operation(summary = "查询所有广告数据")
    @GetMapping(value = "/getAllAdvertisement")
    public R getAllAdvertisement() {
        LambdaJoinQueryWrapper<Advertisement> wrapper = new LambdaJoinQueryWrapper<>(Advertisement.class);
        wrapper.innerJoin(AdvertisingPlace.class, AdvertisingPlace::getAdvertisingPlaceId, Advertisement::getAdvertisingPlaceId);
        List<Advertisement> advertisementList = advertisementService.list(wrapper);
        return R.ok("查询所有广告数据成功").data("advertisementList", advertisementList);
    }
    
    @Operation(summary = "增加广告")
    @PostMapping(value = "/addAdvertisement")
    public R addAdvertisement(Advertisement advertisement) {
        boolean flag = advertisementService.save(advertisement);
        if (flag) {
            return R.ok("增加广告成功");
        } else {
            return R.ok("增加广告失败");
        }
    }

    @Operation(summary = "删除广告")
    @GetMapping(value = "/deleteAdvertisement/{advertisementId}")
    public R deleteAdvertisement(@PathVariable("advertisementId") Integer advertisementId) {
        boolean flag = advertisementService.removeById(advertisementId);
        if (flag) {
            return R.ok("删除广告成功");
        } else {
            return R.ok("删除广告失败");
        }
    }

    @Operation(summary = "按照广告查看")
    @GetMapping(value = "/getAdvertisementById/{advertisementId}")
    public R getAdvertisementById(@PathVariable("advertisementId") Integer advertisementId) {
        LambdaJoinQueryWrapper<Advertisement> wrapper = new LambdaJoinQueryWrapper<>(Advertisement.class);
        wrapper.innerJoin(AdvertisingPlace.class, AdvertisingPlace::getAdvertisingPlaceId, Advertisement::getAdvertisingPlaceId);
        wrapper.eq(Advertisement::getAdvertisementId, advertisementId);
        Advertisement advertisement = advertisementService.getOne(wrapper);
        if (advertisement != null) {
            return R.ok("按照广告查看成功").data("advertisement", advertisement);
        } else {
            return R.error("按照广告查看失败");
        }
    }

    @Operation(summary = "修改广告")
    @PostMapping(value = "/updateAdvertisement")
    public R updateAdvertisement(Advertisement advertisement) {
        boolean flag = advertisementService.updateById(advertisement);
        if (flag) {
            return R.ok("修改广告成功");
        } else {
            return R.ok("修改广告失败");
        }
    }
}